<?php
class Backend_Mediaconfig_Controller extends Backend_Controller
{ 
	
	public function indexAction()
	{
        $resource = Resource::getInstance();
        $conf = Model_Medialib::getInstance()->getConfig()->__toArray();
        $this->_resource->addJs('/js/app/system/crud/'.strtolower($this->_module).'.js'  , 4);

         $this->_resource->addInlineJs('
        	var canEdit = '.((boolean)$this->_user->canEdit($this->_module)).';
        	var canDelete = '.((boolean)$this->_user->canDelete($this->_module)).';
        	app.imageSize = '.json_encode($conf['image']['sizes']).';      	
        ');
    }
    
    public function listAction()
    {
    	$media = Model_Medialib::getInstance();
    	$config = $media->getConfig()->__toArray();
    	
    	$result = array();
    	
    	foreach ($config['image']['sizes'] as $code => $item){
    		$resize = 'crop';
    		if(isset($config['image']['thumb_types'][$code]))
    			$resize = $config['image']['thumb_types'][$code];
    			
    		$result[] = array(
    			'code'=>$code,
    			'resize'=>$resize,
    			'width'=>$item[0],
    			'height'=>$item[1]
    		);
    	}
    	Response::jsonSuccess($result);
    }
    
    public function updateAction(){
    	
    	if(!User_Admin::getInstance()->canEdit($this->_module))
            Response::jsonError($this->_lang->CANT_MODIFY);
            
    	$data = Request::post('data', 'string',false);
    	
    	if($data === false)
    		Response::jsonSuccess();
    		
    	$dataType = json_decode($data);
    	if(!is_array($dataType)){
    		$data = array(json_decode($data , true));
    	}else{
    		$data = json_decode($data , true);
    	}	
    	
    	$media = Model_Medialib::getInstance();
    	$configImage = $media->getConfig()->get('image');

    	foreach ($data as $item){
    		$code = Filter::filterValue('pagecode', $item['code']);
    		$configImage['sizes'][$code] = array(intval($item['width']) , intval($item['height']));
    		$configImage['thumb_types'][$code] = $item['resize'];
    	}
    	
    	$config = $media->getConfig();
    	$config->set('image', $configImage);
    	
    	if(!$config->save())
    		Response::jsonError($this->_lang->CANT_WRITE_FS);
    		
    	Response::jsonSuccess();	
    }
    
    public function deleteAction()
    {
    	if(!User_Admin::getInstance()->canEdit($this->_module))
            Response::jsonError($this->_lang->CANT_MODIFY);
            
    	$data = Request::post('data', 'string',false);
    	
    	if($data === false)
    		Response::jsonSuccess();
    		
    	$dataType = json_decode($data);
    	if(!is_array($dataType)){
    		$data = array(json_decode($data , true));
    	}else{
    		$data = json_decode($data , true);
    	}	
    	
    	$media = Model_Medialib::getInstance();
    	$configImage = $media->getConfig()->get('image');

    	foreach ($data as $item){
    		$code = Filter::filterValue('pagecode', $item['code']);
    		unset($configImage['sizes'][$code]);
    		unset($configImage['thumb_types'][$code]);
    	}
    	
    	$config = $media->getConfig();
    	$config->set('image', $configImage);
    	
    	if(!$config->save())
    		Response::jsonError($this->_lang->CANT_WRITE_FS);
    		
    	Response::jsonSuccess();	
    }
    
    public function startcropAction()
    { 	
    	if(!User_Admin::getInstance()->canEdit($this->_module))
            Response::jsonError($this->_lang->CANT_MODIFY);
            
        $notCroped = Request::post('notcroped', 'boolean', false);
        $sizes = Request::post('size', 'array', array());
        
        if(empty($sizes) || !is_array($sizes))
        	Response::jsonError($this->_lang->MSG_SELECT_SIZE);
        

        $mediaConfig = Model_Medialib::getInstance()->getConfig()->__toArray();
        $acceptedSizes = array_keys($mediaConfig['image']['sizes']);
        $sizeToCrop = array();
        
        
        foreach ($sizes as $key=>$item)
        	if(in_array($item, $acceptedSizes,true))
        		$sizeToCrop[] = $item;
             
        if(empty($sizeToCrop))
        	Response::jsonError($this->_lang->MSG_SELECT_SIZE); 
             
        $bgStorage = new Bgtask_Storage_Orm(Model::factory('bgtask'),Model_Bgtask_Signal::getInstance());
        $logger = new Bgtask_Log_File('./log/recrop'.date('YmdHis').'.txt');
        $tm = Bgtask_Manager::getInstance();
        $tm->setStorage($bgStorage);
        $tm->setLogger($logger);
        $tm->launch(Bgtask_Manager::LAUNCHER_JSON, 'Task_Recrop' , array('types'=>$sizeToCrop,'notCroped'=>$notCroped));
    }
}
    